/**
 * Copyright (C) 2008 GeoScheduler Team, as stated on <http://www.geoscheduler.org/authors>.
 * 
 * This file is part of GeoScheduler.
 * 
 * GeoScheduler is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * GeoScheduler is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with GeoScheduler. If not, see <http://www.gnu.org/licenses/>.
 */

package org.geoscheduler.rest;

import java.util.Collections;

import javax.annotation.Resource;
import javax.ws.rs.ConsumeMime;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.ProduceMime;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.geoscheduler.commons.msgs.ParametrizedMessage;
import org.geoscheduler.commons.msgs.types.EventMsgs;
import org.geoscheduler.commons.msgs.types.GeneralMsgs;
import org.geoscheduler.dao.EventDAO;
import org.geoscheduler.exceptions.JSONWrappedException;
import org.geoscheduler.model.Account;
import org.geoscheduler.model.Event;
import org.geoscheduler.rest.utils.MIMEConstants;
import org.json.JSONObject;

/**
 * 
 * <h2>EventsAccountEidResource</h2>
 *
 * @author lko - basic implementation
 * @author srakyi - refactorings and change to {@link AbstractAccountDependantResource}
 */
@Resource
@Path("events/{login}/{eid}")
public class EventsAccountEidResource extends AbstractAccountDependantResource {

	private final Log LOG = LogFactory.getLog(EventsResource.class);

	public EventsAccountEidResource() {
		super();
	}

	@GET
	@ConsumeMime(MIMEConstants.APP_FORM_URLENCODED)
	@ProduceMime(MIMEConstants.APP_JSON)
	public JSONObject getEvent(@PathParam("login") String login, @PathParam("eid") Long eid) {
		try {
			Account account = loadAccount(login);
			Event event = getEventDAO().loadById(eid);
			if (event != null && !account.getId().equals(event.getIdAccount())) {
				throw new JSONWrappedException(EventMsgs.EVENT_DOES_NOT_BELONG_TO_ACCOUNT, event.getId(), account.getId());
			}
			if (event == null) {
				throw new JSONWrappedException(GeneralMsgs.NOT_FOUND, eid);
			}
			return response(event);
		} catch (JSONWrappedException ex) {
			LOG.error("failed to get event for account " + login, ex);
			return response(ex);
		} catch (Exception ex) {
			LOG.error("failed to get event for account " + login, ex);
			return response(new ParametrizedMessage(GeneralMsgs.UNKNOWN_ERROR, ex.getMessage()));
		}
	}

	@DELETE
	@ConsumeMime(MIMEConstants.APP_FORM_URLENCODED)
	@ProduceMime(MIMEConstants.APP_JSON)
	public JSONObject deleteEvent(@PathParam("login") String login, @PathParam("eid") Long eid) {
		try {
			Account account = loadAccount(login);
			EventDAO eventDAO = getEventDAO();
			if (eventDAO.isOwnedByIdAccount(eid, account.getId())) {
				throw new JSONWrappedException(EventMsgs.EVENT_DOES_NOT_BELONG_TO_ACCOUNT, eid, account.getId());
			}
			int count = eventDAO.delete(eid);
			return response(Collections.singletonMap("deleted", count));
		} catch (Exception ex) {
			LOG.error("failed to read event for account " + login, ex);
			return response(ex);
		}
	}
}
